package cn.chi.org.controller;

import cn.chi.org.domain.Department;
import cn.chi.org.query.DepartmentQuery;
import cn.chi.org.service.IDepartmentService;
import cn.chi.util.AjaxResult;
import cn.chi.util.PageInfo;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Cyf
 */
@RestController
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private IDepartmentService departmentService;
    @GetMapping
    public List<Department> list(){
        return departmentService.queryAll();
    }
    @PutMapping
    public AjaxResult saveOrUpdate(@RequestBody Department department){
        try {
            if (department.getId()==null){
                departmentService.save(department);
            }else {
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    @GetMapping("/{id}")
    public Department findById(@PathVariable("id") Long id){
        return departmentService.queryById(id);
    }
    @PostMapping
    public PageInfo<Department> queryAll(@RequestBody DepartmentQuery departmentQuery){
        return departmentService.queryPage(departmentQuery);
    }

    @PatchMapping
    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            departmentService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }
    /*获取树*/
    @GetMapping("/deptTree")
    public List<Department> getTree(){
        return departmentService.getTree();
    }
}
